package com.yjf.ljr.util;

import android.content.Context;
import android.media.MediaScannerConnection;
import android.net.Uri;

public class MediaScanner implements MediaScannerConnection.MediaScannerConnectionClient{

	/** 
     * 扫描对象 
     */  
    private MediaScannerConnection mediaScanConn = null;  
  
    public MediaScanner(Context context) {  
        //实例化  
        mediaScanConn = new MediaScannerConnection(context, this);  
    }  
  
    /**文件路径集合**/  
    private String[] filePaths;  
    /**文件MimeType集合**/  
    private String[] mimeTypes;  
	
	@Override
	public void onMediaScannerConnected() {
		for (int i = 0; i < filePaths.length; i++) {  
            mediaScanConn.scanFile(filePaths[i], mimeTypes[i]);//服务回调执行扫描  
        }  
        filePaths = null;  
        mimeTypes = null;		
	}

	@Override
	public void onScanCompleted(String path, Uri uri) {
		try{
			mediaScanConn.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	
	/** 
     * 扫描文件 
     * @author YOLANDA 
     * @param filepaths 
     * @param mimeTypes 
     */  
    public void scanFiles(String[] filePaths, String[] mimeTypes) {  
        this.filePaths = filePaths;  
        this.mimeTypes = mimeTypes;  
        mediaScanConn.connect();//连接扫描服务  
    }  
	
}
